package org.example.controller;

import org.example.domain.Address;
import org.example.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/response")
public class ResponseController {

    //响应、跳转页面
    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("ResponseController-toJumpPage..");
        return "page.jsp";
    }

    //响应文本
    @RequestMapping("/toText")
    @ResponseBody
    public String toText() {
        System.out.println("ResponseController-toText..");
        return "response-text";
    }

    //响应json-pojo
    @RequestMapping("/toJsonPojo")
    @ResponseBody
    public User toJsonPojo() {
        User user = new User();
        user.setName("root");
        user.setPassword("123456");
        Address address = new Address();
        address.setCity("hangzhou");
        address.setProvince("zhejiang");
        user.setAddress(address);
        System.out.println("ResponseController-toJsonPojo.." + user);
        return user;
    }

    //响应pojo集合
    @RequestMapping("/toJsonPojoList")
    @ResponseBody
    public List<User> toJsonPojoList() {
        User user1 = new User();
        user1.setName("root");
        user1.setPassword("123456");
        Address address1 = new Address();
        address1.setCity("hangzhou");
        address1.setProvince("zhejiang");
        user1.setAddress(address1);

        User user2 = new User();
        user2.setName("admin");
        user2.setPassword("abc123");
        Address address2 = new Address();
        address2.setCity("hangzhou");
        address2.setProvince("zhejiang");
        user2.setAddress(address2);

        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("ResponseController-toJsonPojoList.." + users);
        return users;
    }
}

